package com.example.android_wx.adapter;

import android.text.Html;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatTextView;
import androidx.recyclerview.widget.RecyclerView;

import com.example.android_wx.R;
import com.example.android_wx.http.CircleBean;
import com.luck.picture.lib.entity.LocalMedia;

import java.util.ArrayList;
import java.util.List;

public class CircleCommitAdapter extends RecyclerView.Adapter<CircleCommitAdapter.Holder> {

    private List<CircleBean.CommitDataDTO> arrayList;
    private OnItemClickListener onItemClickListener = null;

    public CircleCommitAdapter(List<CircleBean.CommitDataDTO> arrayList) {
        this.arrayList = arrayList;
    }

    @NonNull
    @Override
    public Holder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_circle_commit, parent, false);
        return new Holder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull Holder holder, int position) {

        CircleBean.CommitDataDTO bean = arrayList.get(position);
        System.out.println(bean);

        holder.nameTextView.setText(bean.getMemberNickname());

        if (bean.getReplyId() != -1) {
            holder.nameTextView.append("回复" + bean.getReplyMemberNickname());
        }
        holder.nameTextView.append(":" + bean.getCommitContent());

        holder.nameTextView.setOnClickListener(view -> {
            if (onItemClickListener != null) onItemClickListener.onClick(position, bean);
        });

        holder.nameTextView.setOnLongClickListener(view -> {
            if (onItemClickListener != null) onItemClickListener.onLongClick(position, bean);
            return false;
        });

        String name = "<font color='#586b95'><b>" + bean.getMemberNickname() + "</b></font>";
        holder.nameTextView.setText(Html.fromHtml(name));

        if (bean.getReplyId() != -1) {
            String append = "<font  color='#000000'>回复</font>";
            String append1 = "<font color='#586b95'><b>" + bean.getReplyMemberNickname() + "</b></font>";
            holder.nameTextView.setText(Html.fromHtml(name + append + append1));

        }
        holder.nameTextView.append("：" + bean.getCommitContent());

    }

    @Override
    public int getItemCount() {
        return arrayList.size();
    }

    public void setOnItemClickListener(OnItemClickListener onItemClickListener) {
        this.onItemClickListener = onItemClickListener;
    }



    //列表项单击接口
    public interface OnItemClickListener {

        //列表项整体单击
        void onClick(int position, CircleBean.CommitDataDTO bean);


        //列表项整体长按
        void onLongClick(int position, CircleBean.CommitDataDTO bean);
    }



    public static class Holder extends RecyclerView.ViewHolder {

        private AppCompatTextView nameTextView;

        public Holder(@NonNull View view) {
            super(view);

            nameTextView = view.findViewById(R.id.nameTextView);

        }

    }

}
